<script setup lang="ts">
  import { ref, defineProps, reactive, defineExpose } from "vue"
  import { createMessage } from '@/Public-UIlibrary/pb-message/method'
  import { getTenantAreaEdit, getTenantAreaAdd } from '@/api/System'
  let emits = defineEmits('onConfirmModal')
  const props = defineProps({
    areaTree: {
      type: Array,
      default: () => []
    }
  })
  let loading = ref(false)
  const initialFrom = {
    parentAreaName: "",
    areaId: "",
    areaCode: "",
    parentAreaId: '',
    areaName: "",
    areaAddress: '',
    description: ""

  }
  let areaForm = ref({
    ...initialFrom
  })

  const areaFormRef = ref()
  let areaModalShow = ref(false)
  const onShow = (row) => {
    if (row) {
      let { parentareaName, createTime, ...needFrom } = row
      areaForm.value = { ...needFrom }
    }
    areaModalShow.value = true
  }
  // 关闭表单重置
  const cancelFn = () => {
    areaFormRef.value.resetFields();
    Object.assign(areaForm.value, initialFrom);
  }
  // 确认保存
  const onConfirmFn = () => {
    areaFormRef.value.validate((valid) => {
      if (valid) {
        if (areaForm.value.areaId) {
          getTenantAreaEditApi()
        } else {
          getTenantAreaAddApi()
        }

      } else {

      }
    })
  }
  // 新增区域 
  const getTenantAreaAddApi = async () => {
    loading.value = true
    let res = await getTenantAreaAdd({ ...areaForm.value })
    loading.value = false
    if (res.code == 2000) {
      createMessage({
        message: '新增成功',
        type: 'success',
      })
      areaModalShow.value = false
      emits('onConfirmModal')
    }
  }
  // 修改区域
  const getTenantAreaEditApi = async () => {
    loading.value = true
    let res = await getTenantAreaEdit({ ...areaForm.value })
    loading.value = false
    if (res.code == 2000) {
      createMessage({
        message: '编辑成功',
        type: 'success',
      })
      areaModalShow.value = false
      emits('onConfirmModal')
    }


  }
  defineExpose({ onShow })
</script>

<template>
  <pb-popup v-model="areaModalShow" :title="areaForm.areaId ? '编辑' : '新增'" Width="365" @confirmFn="onConfirmFn"
    @cancelFn="cancelFn">
    <el-form :model="areaForm" ref="areaFormRef" label-width="auto" v-loading="loading">
      <el-form-item label="区域编号" prop="areaCode" :rules="[
        {
          required: true,
          pattern: /^[0-9]{4,16}$/,
          message: '请输入4-16位数字',

          trigger: 'blur',
        }, ]">
        <pb-input v-model=" areaForm.areaCode" placeholder="请输入区域编号" />
      </el-form-item>
      <el-form-item label="上级区域" prop="parentTenantId">
        <pb-tree-select v-if="!areaForm.areaId " v-model="areaForm.parentAreaId" :Data="areaTree" placeholder="请选择上级区域"
          width="240" :defaultProps="{ children: 'children', label: 'areaName', value: 'areaId' }" />
        <pb-input v-else disabled v-model=" areaForm.parentAreaName" placeholder="请选择上级区域" />
      </el-form-item>
      <el-form-item label="区域名称" prop="areaName" :rules="[
                 {
          required: true,
          pattern: /^[\u4e00-\u9fa5A-Za-z0-9]{2,16}$/,
          message: '请输入2-16位中文、数字、字母',

          trigger: 'blur',
        },  ]">
        <pb-input v-model=" areaForm.areaName" placeholder="请输入区域名称" />
      </el-form-item>
      <el-form-item label="区域地址" prop="areaAddress" :rules="[
                 {
     
          pattern: /^[\u4e00-\u9fa5A-Za-z0-9]{2,32}$/,
          message: '请输入2-32位中文、数字、字母',

          trigger: 'blur',
        },  ]">
        <pb-input v-model=" areaForm.areaAddress" placeholder="请输入区域地址" />
      </el-form-item>
      <el-form-item label="备注" prop="description">
        <pb-input v-model="areaForm.description" placeholder="请输入备注" type="textarea" />
      </el-form-item>
    </el-form>
  </pb-popup>
</template>

<style lang="less" scoped>

</style>